/**
 * date: 2022/1/5
 * description:
 * 把packages目录下左右的package进行打包
 * author: Aren
 */

const fs = require('fs');
const execa = require('execa'); // 开启子进程进行打包， 最终还是使用rollup来打包

const targets = fs.readdirSync('packages').filter((file) => {
    return fs.statSync(`packages/${file}`).isDirectory()
});


// 对所有目标进行依次打包，并且是并行打包

async function build(target) {
    await execa('rollup', [ // rollup -c --environment TARGET:shared
        '-c', '--environment',
        `TARGET:${target}`
    ], {
        stdio: 'inherit' // 当子进程的打包信息共享给父进程
    });
}

function runParallel(targets, iteratorFn) {
    const res = [];
    for (const item of targets) {
        const p = iteratorFn(item);
        res.push(p);
    }
    return Promise.all(res)
}

runParallel(targets, build)